package com.stb.server.url;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Encrypter {
	public static String computeHash(String string) {
		MessageDigest digest;
		try {
			digest = MessageDigest.getInstance("SHA-1");
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException(e);
		}
		digest.reset();
		digest.update(string.getBytes());
		return byteArrayToHexString(digest.digest());
	}

	private static String byteArrayToHexString(byte[] b) {
		StringBuffer sb = new StringBuffer(b.length * 2);
		for (int i = 0; i < b.length; i++) {
			int v = b[i] & 0xff;
			if (v < 16) {
				sb.append('0');
			}
			sb.append(Integer.toHexString(v));
		}
		return sb.toString();
	}
}
